import 'package:flutter/material.dart';
import 'package:jubobao/profile/addressBook/JuBoAddressBookServices.dart';
import 'package:jubobao/profile/addressBook/JuBoDepermentModel.dart';
import 'package:jubobao/profile/addressBook/JuBpEmployeeModel.dart';

class JuBoAddressBook extends StatefulWidget {

  static final String routeName = 'addressBook';
  @override
  _JuBoAddressBookState createState() => _JuBoAddressBookState();
}

class _JuBoAddressBookState extends State<JuBoAddressBook> {

  List <DepList>_depermentResult = [];
  List <ReList>_employeeResult = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    JuBoAddressBookServices.depermentRequest().then((value){
       print(value);
       setState(() {
         _depermentResult = value;
       });
    });
    
    JuBoAddressBookServices.employeeList().then((value) {
        setState(() {
          _employeeResult = value;
        });
    });
  }

  @override
  Widget build(BuildContext context) {
    return
       DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text('通信录'),
            bottom: TabBar(

              tabs: <Widget>[
                Tab(text: '按部门',),
                Tab(text: '按员工',),
              ],
            ),
          ),
          body: TabBarView(
              children: <Widget>[
                department(),
                roles(),
              ],
          ),
        ),
      );
  }

  Widget department(){

    return ListView.separated(
      itemBuilder: (ctx,index){
        DepList model = _depermentResult[index];
        return InkWell(
          child: ListTile(
            leading: Text(
                "${model.roleName.trimRight()} (${model.numberOfEmployees})"),
            trailing: Icon(
              Icons.arrow_forward_ios,
              size: 18,
            ),
          ),
        );
        },
      itemCount: _depermentResult.length,
      separatorBuilder: (ctx,index){
        return Divider(height: 0.01,);
      },
    );
  }

  Widget roles(){
    return ListView.separated(
      itemBuilder: (ctx,index){
        
        ReList model = _employeeResult[index];

        return InkWell(
          child: ListTile(
            leading: ClipOval(child:
            Image.network(
              model.pic1,
              width: 40,
              height: 40,
              fit: BoxFit.cover,
            )
            ),
            title: Text(model.linkMan),
            subtitle: Text(model.role),
            trailing: Icon(Icons.phone_in_talk),
          ),
          onTap: () {
            Navigator.of(context).pop(model);
          },
        );
      },
      itemCount: _employeeResult.length,
      separatorBuilder: (ctx,index){
        return Divider(height: 0.01,);
      },
    );
  }
}
